package com.xbongbong.sys.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.sys.domain.dao.UserDepartmentDao;
import com.xbongbong.sys.domain.entity.DepartmentEntity;
import com.xbongbong.sys.domain.entity.UserDepartmentEntity;
import com.xbongbong.sys.model.UserDepartmentModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: wufeng
 * @date: 2018/9/12 14:31
 * @desrcption:
 */
@Service("userDepartmentModel")
public class UserDepartmentModelImpl implements UserDepartmentModel {

    private static final String COLUMNS = "corpid, user_id, department_id, sort, is_leader, add_time, update_time, del";

    @Resource
    private UserDepartmentDao userDepartmentDao;

    @Override
    public Integer insert(UserDepartmentEntity entity) throws XbbException {
        return userDepartmentDao.insert(entity);
    }

    @Override
    public Integer update(UserDepartmentEntity entity) throws XbbException {
        return userDepartmentDao.update(entity);
    }

    @Override
    public List<UserDepartmentEntity> findEntitys(Map<String, Object> params) {
        params.put("columns", new StringBuffer(COLUMNS));
        return userDepartmentDao.findEntitys(params);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return userDepartmentDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public List<String> getDepManagerList(DepartmentEntity department) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", department.getCorpid());
        param.put("del", 0);
        param.put("isLeader", 1);
        param.put("departmentId", department.getId());
        List<UserDepartmentEntity> userDepartmentList = findEntitys(param);

        List<String> managerList = new ArrayList<>();
        for (UserDepartmentEntity entity : userDepartmentList) {
            String managerId = entity.getUserId();
            if (!managerList.contains(managerId)) {
                managerList.add(managerId);
            }
        }
        return managerList;
    }

    @Override
    public List<Long> getDepIds(Map<String, Object> param) {
        return userDepartmentDao.getDepIds(param);
    }

    @Override
    public Integer clearUnauthUserByUpdateTime(String corpid, Integer updateTime) {
        return userDepartmentDao.clearUnauthUserByUpdateTime(corpid, updateTime);
    }

    @Override
    public Integer deleteByCorpId(String corpid) {
        return userDepartmentDao.deleteByCorpId(corpid);
    }

    @Override
    public Integer deleteByDepIds(List<Long> depIds, String corpid) {
        return userDepartmentDao.deleteByDepIds(depIds, corpid);
    }

    @Override
    public Integer deleteByUserId(String userId, String corpid) {
        return userDepartmentDao.deleteByUserId(userId, corpid);
    }

}
